﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Paper.Entiry;
using System.Collections.Generic;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;

public partial class Theme_3_MyProjectResource : PowerCreator.LMS.Web.Base.WebBasePage
{
    PowerCreator.LMS.Web.Project.Project opProject = new PowerCreator.LMS.Web.Project.Project();
    PowerCreator.LMS.Web.Project.ProjectUser opProjectUser = new PowerCreator.LMS.Web.Project.ProjectUser();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Paper.ManagerPaper opPaper = new PowerCreator.LMS.Web.Paper.ManagerPaper();
    PowerCreator.LMS.Web.Project.ProjectResource opProjectResource = new ProjectResource();
    NewProjectPlan opProjectPlan = new NewProjectPlan();
    PowerCreator.LMS.Web.Course.Resource opResource = new Resource();

    string _openurlcourseware = "/Theme/1/ProjectOpenCourseware.aspx";  //打开课件

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "学习中心----我的方案";
        if (!IsPostBack)
        {
            string classid = Request.QueryString["ClassID"].ToString();
            ViewState["UserID"] = "0";
            ViewState["ProjectClassID"] = classid;
            ViewState["ProjectID"] = "0";
            BinderPageData();
            string search = GetSearch();
            BinderRPT(search);
        }
    }

    protected override void InitPage()
    {
       // base.InitPage();
    }
    private void BinderPageData()
    {
        //初始化界面信息
        PowerCreator.LMS.Project.Entity.VOTTrainClass voClass = new PowerCreator.LMS.Project.Entity.VOTTrainClass();
        voClass = opProjectUser.LoadProjectClass(int.Parse(ViewState["ProjectClassID"].ToString()));
        if (voClass.TrainclaID > 0)
        {
            txt_ClassName.Text = voClass.TrainclaName;
            txt_ClassName.Enabled = false;
            PowerCreator.LMS.Project.Entity.VOTTrainProject voProject = new PowerCreator.LMS.Project.Entity.VOTTrainProject();
            voProject = opProject.LoadTrainProject(voClass.FKTrainclaTrainproID);

            if (voProject.TrainproID > 0)
            {
                ViewState["ProjectID"] = voProject.TrainproID.ToString();
                txt_ProjectName.Text = voProject.TrainproName;
                txt_ProjectName.Enabled = false;
            }
        }
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] != null)
        {
            PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            ViewState["UserID"] = login.UserID.ToString();
        } 
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(string.Format("FKProresTrainproID = {0}", ViewState["ProjectID"]));
        return sbSearch.ToString();
    }
    private void BinderRPT(string search)
    {
        DataTable dt = opProjectResource.QueryProjectResource(search);
        List<ProjectResourceInfo> list = FillList(dt);
        rpt_ProjectResource.DataSource = list;
        rpt_ProjectResource.DataBind();
    }

    private List<ProjectResourceInfo> FillList(DataTable dt)
    {
        List<ProjectResourceInfo> list = new List<ProjectResourceInfo>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ProjectResourceInfo item = new ProjectResourceInfo();
            item.ProjectID = int.Parse(ViewState["ProjectID"].ToString());
            item.ProjectResourceProresClass = int.Parse(dt.Rows[i]["ProresClass"].ToString());
            item.ProjectResourceID = int.Parse(dt.Rows[i]["ProresID"].ToString());
            item.ProjectResourceProresClassName = opProjectResource.GetProjectResourceProresClass(item.ProjectResourceProresClass);
            item.ProresResIsQueue = opProjectResource.GetProjectResourceProresResIsQueue(int.Parse(dt.Rows[i]["ProresResIsQueue"].ToString()));

            string searchProjectResourceState = "FKSpcoelogTrainproID = {0} and FKSpcoelogTrainclaID = {1} and FKSpcoelogUserID = {2} and FKSpcoelogProresID = {3} and FKSpcoelogCourseID = {4} and FKSpcoelogPaperID = {5} ";
            
            //默认设定状态
            item.ProjectResourcePassState = 4;
            item.ProjectResourcePassStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePassState, ProjectPlan.ProjectResourcePlan);

            if (item.ProjectResourceProresClass == Convert.ToInt16(ProjectResourceProresClass.Course))//课程
            {
                item.ProresCourseResIsQueue = opProjectResource.GetProjectResourceProresCourseResIsQueue(int.Parse(dt.Rows[i]["ProresCourseResIsQueue"].ToString()));
                item.ProresPaperMakeupNum = "-";
                item.StartTime = "-";
                item.EndTime = "-";
                item.ResourceID = int.Parse(dt.Rows[i]["FKProresCourseID"].ToString());
                item.ResourceCount = 0;
                item.ProresCoursePassCondition = dt.Rows[i]["ProresCoursePassCondition"].ToString();
                item.ProresPaperPassScore = "-";
                VOTOCourse vo = opCourse.LoadVOTCourse(item.ResourceID);
                if (vo != null)
                {
                    item.ResourceCount = vo.CourseResourceNum;
                    item.ResourceName = vo.CourseName;
                }
                 //设置学习状态
                string t1 = string.Format(searchProjectResourceState, ViewState["ProjectID"], ViewState["ProjectClassID"], ViewState["UserID"],item.ProjectResourceID,item.ResourceID,0);
                DataTable dtt = opProjectPlan.QueryProjectResource(t1,1,1);
                if (dtt.Rows.Count == 1)
                {
                    item.ProjectResourcePassState = int.Parse(dtt.Rows[0]["SpcoelogState"].ToString());
                    item.ProjectResourcePassStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePassState, ProjectPlan.ProjectResourcePlan);
                    //item.ProjectResourcePassStateName += "--" + dtt.Rows[0]["SpcoelogPassPercent"].ToString();
                }
            }
            else if (item.ProjectResourceProresClass == Convert.ToInt16(ProjectResourceProresClass.Paper))  //考试
            {
                item.ProresCourseResIsQueue = "-";
                item.ProresPaperMakeupNum = dt.Rows[i]["ProresPaperMakeupNum"].ToString();
                item.ResourceCount = 1;
                item.ResourceID = int.Parse(dt.Rows[i]["FKProresPaperID"].ToString());
                item.StartTime = dt.Rows[i]["ProresPaperStartTime"].ToString();
                item.EndTime = dt.Rows[i]["ProresPaperEndTime"].ToString();
                item.ProresPaperPassScore = dt.Rows[i]["ProresPaperPassScore"].ToString();
                item.ProresCoursePassCondition = "-";
                
                VOPaper vo = opPaper.LoadPaper(item.ResourceID);
                if (vo != null)
                {
                    item.ResourceName = vo.PaperName;
                }
                string t1 = string.Format(searchProjectResourceState, ViewState["ProjectID"], ViewState["ProjectClassID"], ViewState["UserID"], item.ProjectResourceID, 0, item.ResourceID);
                DataTable dtt = opProjectPlan.QueryProjectResource(t1, 1, 1);
                if (dtt.Rows.Count == 1)
                {
                    item.ProjectResourcePassState = int.Parse(dtt.Rows[0]["SpcoelogState"].ToString());
                    item.ProjectResourcePassStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePassState, ProjectPlan.ProjectResourcePlan);
                }
            }

            list.Add(item);
        }
        return list;
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {


    }

    protected void rpt_ProjectResource_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "lshow")
        {
            for (int i = 0; i < rpt_ProjectResource.Items.Count; i++)
            {
                Panel pl_ResourceList = (Panel)rpt_ProjectResource.Items[i].FindControl("pl_ResourceList");
                pl_ResourceList.Visible = false;
            }
            Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

            Panel pl_ResourceList1 = (Panel)e.Item.FindControl("pl_ResourceList");
            pl_ResourceList1.Visible = true;
            Repeater rpt_ResourceList = (Repeater)e.Item.FindControl("rpt_ResourceList");
            VOTProjectResource voProjectResource = new VOTProjectResource();
            voProjectResource = opProjectResource.LoadProjectResource(int.Parse(e.CommandArgument.ToString()));
            List<CourseResourceInfo> list = new List<CourseResourceInfo>();
           
            NewProjectPlanInfo info = new NewProjectPlanInfo();
            info.ProjectID = int.Parse(ViewState["ProjectID"].ToString());
            info.ClassID = int.Parse(ViewState["ProjectClassID"].ToString());
            info.UserID = int.Parse(ViewState["UserID"].ToString());

            if (voProjectResource.ProresClass == Convert.ToInt16(ProjectResourceProresClass.Course))
            {
                //显示课程资源
                string search = string.Format(" FKCourseresCourseID = {0}", voProjectResource.FKProresCourseID);

                DataTable dt = opCourse.QueryCourseResource(search, opCourse.QueryCourseResourceCount(search), 1);
                if (dt.Rows.Count != 0)
                {
                    dt.Columns.Add("ResourceSysType1");
                    dt.Columns.Add("OpenUrl1");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        CourseResourceInfo item = new CourseResourceInfo();
                        item.CourseID = voProjectResource.FKProresCourseID;
                        item.CourseresID = int.Parse(dt.Rows[i]["CourseresID"].ToString());
                        item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersResourceID"].ToString());
                        item.ProjectResourceID = voProjectResource.ProresID;
                        VOTCourseResource voCourseResource = new VOTCourseResource();
                        voCourseResource = opCourse.LoadCourseResource(item.CourseresID);
                        item.CoursersClassID = voCourseResource.CoursersClass;
                        item.CoursersClassName = opCourse.GetCoursersClassByData(item.CoursersClassID, GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page));
                        //用户学习状态
                        item.ProjectResourcePlanState = 4;
                       // item.ProjectResourcePlanStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePlanState, ProjectPlan.ProjectCourseResourcePlan);

                        if (voCourseResource.CoursersClass == Convert.ToInt16(CoursersClass.Courseware))
                        {
                            VOTResource voResource = opCourse.LoadResource(item.CoursersResourceID);
                            if (voResource.ResourceID > 0)
                            {
                                item.ResourceName = voResource.ResourceName;
                            }
                            else
                            {
                                item.ResourceName = "课件资源己被删除";
                            }
                            
                        }
                        else if (voCourseResource.CoursersClass == Convert.ToInt16(CoursersClass.Paper))
                        {
                            item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersPaperID"].ToString());
                            VOPaper voPaper = new VOPaper();
                            voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                            if (voPaper.PaperID > 0)
                            {
                                item.ResourceName = voPaper.PaperName;
                            }
                            else
                            {
                                item.ResourceName = "试卷资源己被删除";
                            }
                            
                        }
                        
                        VOTStudentPassCourseresourceLog vitem = opProjectPlan.QueryCourseResourcePlanState(info, item);
                        if (vitem != null)
                            item.ProjectResourcePlanState = vitem.SscrlogStatu;
                        //item.ProjectResourcePlanState = state;
                        item.ProjectResourcePlanStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePlanState, ProjectPlan.ProjectCourseResourcePlan);

                        list.Add(item);
                    }
                    rpt_ResourceList.DataSource = list;
                    rpt_ResourceList.DataBind();
                }
                else
                {
                    pl_ResourceList1.Visible = false;
                }
            }
            else if (voProjectResource.ProresClass == Convert.ToInt16(ProjectResourceProresClass.Paper))
            {
                Label lbl_rpt_msg = (Label)e.Item.FindControl("lbl_rpt_msg");
                ////需要判断是否补考
                string bukao = "0"; //是否补考0：不是，1：是	
                int exampapercount = opProjectPlan.QueryExamPaperCount(voProjectResource, info, 0);
                if (exampapercount != 0)
                    bukao = "1";
                if (exampapercount == 0)
                {
                    string url = string.Format(WebUtils.WebRoot + "/web/UserObject/OpenPaperList.aspx?ProjectID={0}&ProjectClassID={1}&CourseID={2}&IsBuKao={3}&ProjectResourceID={4}&CourseResourceID={5}&ResourceID={6}", ViewState["ProjectID"], ViewState["ProjectClassID"], voProjectResource.FKProresCourseID, bukao, voProjectResource.ProresID, "0", voProjectResource.FKProresPaperID);
                    Script.NewOpenURL(url, "openpaper");
                }
                else
                {
                    if (voProjectResource.ProresPaperMakeupNum <= exampapercount)
                    {
                        lbl_rpt_msg.Text = "己超过最大考试次数!";
                       // LinkButton lnkbtn_show = (LinkButton)e.Item.FindControl("lnkbtn_show");
                      //  lnkbtn_show.Enabled = false;
                    }
                    else
                    {
                        string url = string.Format(WebUtils.WebRoot + "/web/UserObject/OpenPaperList.aspx?ProjectID={0}&ProjectClassID={1}&CourseID={2}&IsBuKao={3}&ProjectResourceID={4}&CourseResourceID={5}&ResourceID={6}", ViewState["ProjectID"], ViewState["ProjectClassID"], voProjectResource.FKProresCourseID, bukao, voProjectResource.ProresID, "0", voProjectResource.FKProresPaperID);
                        Script.NewOpenURL(url, "openpaper");
                    }
                }
            }
        }
    }
    protected void rpt_ProjectResourceList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "lstudy")
        {
            string projectresourceid = "0";
            string courseresourceid = "0";
            for (int i = 0; i < rpt_ProjectResource.Items.Count; i++)
            {
                Panel pl_ResourceList = (Panel)rpt_ProjectResource.Items[i].FindControl("pl_ResourceList");
                if (pl_ResourceList.Visible)
                {
                    Label lbl_ProjectResourceID = (Label)rpt_ProjectResource.Items[i].FindControl("lbl_ProjectResourceID");
                    projectresourceid = lbl_ProjectResourceID.Text;
                }
            }
            Label lbl_CourseResourceID = (Label)e.Item.FindControl("lbl_CourseResourceID");
            courseresourceid = lbl_CourseResourceID.Text;

            //课件类型的课程资源
            VOTProjectResource voProjectResource = new VOTProjectResource();
            voProjectResource = opProjectResource.LoadProjectResource(int.Parse(projectresourceid));
                                
            VOTCourseResource voCourseResource = opCourse.LoadCourseResource(int.Parse(courseresourceid));

            Label lbl_rptlist_msg = (Label)e.Item.FindControl("lbl_rptlist_msg");
            if (voCourseResource.CourseresID > 0)
            {
                NewProjectPlanInfo info = new NewProjectPlanInfo();
                info.ProjectID = int.Parse(ViewState["ProjectID"].ToString());
                info.ClassID = int.Parse(ViewState["ProjectClassID"].ToString());
                info.UserID = int.Parse(ViewState["UserID"].ToString());

                if (voCourseResource.CoursersClass == Convert.ToInt16(CoursersClass.Courseware))
                {
                    string url = WebUtils.WebRoot + _openurlcourseware + "?ProjectID=" + ViewState["ProjectID"].ToString() + "&ProjectClassID=" + ViewState["ProjectClassID"].ToString() + "&ProjectResourceID=" + projectresourceid + "&CourseResourceID=" + voCourseResource.CourseresID.ToString() + "&ResourceID=" + voCourseResource.FKCoursersResourceID.ToString();
                    Script.ClickNewOpenUrl(this, url, "open");
                }
                else if (voCourseResource.CoursersClass == Convert.ToInt16(CoursersClass.Paper))
                {
                    //需要判断是否补考
                    string bukao = "0"; //是否补考0：不是，1：是	
                    int exampapercount = opProjectPlan.QueryExamPaperCount(voProjectResource, info, voCourseResource.FKCoursersPaperID);
                    if (exampapercount != 0)
                    {
                        bukao = "1";
                    }
                    if (exampapercount == 0)
                    {
                        string url = string.Format(WebUtils.WebRoot + "/web/UserObject/OpenPaperList.aspx?ProjectID={0}&ProjectClassID={1}&CourseID={2}&IsBuKao={3}&ProjectResourceID={4}&CourseResourceID={5}&ResourceID={6}", ViewState["ProjectID"], ViewState["ProjectClassID"], voCourseResource.FKCourseresCourseID, bukao, projectresourceid, voCourseResource.CourseresID, voCourseResource.FKCoursersPaperID);
                        Script.ClickNewOpenUrl(this, url, "openpaper");
                        //Script.NewOpenURL(url, "openpaper");
                    }
                    else
                    {
                        if (voProjectResource.ProresPaperMakeupNum <= exampapercount)
                        {
                            lbl_rptlist_msg.Text = "己超过最大考试次数!";
                        }
                        else
                        {
                            string url = string.Format(WebUtils.WebRoot + "/web/UserObject/OpenPaperList.aspx?ProjectID={0}&ProjectClassID={1}&CourseID={2}&IsBuKao={3}&ProjectResourceID={4}&CourseResourceID={5}&ResourceID={6}", ViewState["ProjectID"], ViewState["ProjectClassID"], voCourseResource.FKCourseresCourseID, bukao, projectresourceid, voCourseResource.CourseresID, voCourseResource.FKCoursersPaperID);
                            Script.ClickNewOpenUrl(this, url, "openpaper");
                            //Script.NewOpenURL(url, "openpaper");
                        }
                    }
                }
            }
        }
    }
}